# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=SDL_ttf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.11
pkgrel=7
pkgdesc="A library that allows you to use TrueType fonts in your SDL applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://libsdl.org/projects/SDL_ttf/"
msys2_references=(
  "cpe: cpe:/a:libsdl:sdl_ttf"
)
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-SDL"
         "${MINGW_PACKAGE_PREFIX}-freetype")
source=(https://libsdl.org/projects/SDL_ttf/release/${_realname}-${pkgver}.tar.gz
        "001-bug1433.patch"
        "002-freetype-pkgconfig.patch"
        "003-aarch64.patch")
sha256sums=('724cd895ecf4da319a3ef164892b72078bd92632a5d812111261cde248ebcdb7'
            '48b899d97da1fbe44c4e850e5c9781ca278b6e6f1baf43fa165f7061863940c1'
            'a2ecbd0fde86ac265bd7820ba557bb2f10fd6e3fef3156629ca14f45bd80861d'
            '60048f2cc46720409b42f3b61e6544d24dca69870e059550115f04a842ab1617')

apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://bugs.archlinux.org/task/28674
  apply_patch_with_msg \
    001-bug1433.patch

  # Fix build with FreeType 2.9.1
  apply_patch_with_msg \
    002-freetype-pkgconfig.patch

  apply_patch_with_msg \
    003-aarch64.patch

  touch NEWS README AUTHORS ChangeLog
  ./autogen.sh
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
